package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.result.TokenInfo;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("shopping")
public class ShoppingReCommendController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private ICartService cartService;

    @GetMapping("recommend")
    public ResponseData<Set<PanelDto>> recommend() {
        Set<PanelDto> recommendGoods = iProductService.getRecommendGoods();
        ResponseUtil<Set<PanelDto>> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(recommendGoods);
    }

    @GetMapping("carts")
    public ResponseData<List<CartProductDto>> carts(@RequestHeader String user_info) {
        TokenInfo tokenInfo = JSON.parseObject(user_info, TokenInfo.class);
        Long userId = tokenInfo.getUid();
        List<CartProductDto> productDtoList = cartService.getCartListById(userId);
        ResponseUtil<List<CartProductDto>> responseUtil = new ResponseUtil<>();
        return responseUtil.setData(productDtoList);
    }
    @PostMapping("carts")
    public ResponseData carts(@RequestBody CartRequest request){
        cartService.addToCart(request);
        ResponseUtil<String> stringResponseUtil = new ResponseUtil<>();
        return stringResponseUtil.setData("成功");
    }
}
